package com.alipay.service.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author Jun
 * @date 2019/3/22
 */
@Slf4j
@Service
public class CacheService {

    @Autowired
    private BaseCacheService baseCacheService;

    /**
     * 拆分的随机红包插入缓存中
     *
     * @param redBagNo
     * @param details
     */
    public void addRedBags(String redBagNo, List<BigDecimal> details) {
        List<String> values = details.stream().map(detail -> detail.toString()).collect(Collectors.toList());
        baseCacheService.pushValue(redBagNo, values);
    }

    /**
     * 通过Redis队列的原子性抢红包
     * @param redBagNo
     * @return
     */
    public BigDecimal getRedBag(String redBagNo) {
        String value = baseCacheService.getValue(redBagNo);
        if (StringUtils.isNotBlank(value)) {
            return new BigDecimal(value);
        }
        return null;
    }
}
